{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}R:\WSV2\TBu_AKe\Spatial_NEW\spatial_4_spgen_overlap_0102.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}15 Feb 2023, 09:53:29
{txt}
{com}. 
. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. cd // display in log file       
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}.         
. 
.         *************************************
.         ** 1) W matrix, save vertical only **
.         *************************************
. 
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan\spatial_3_wmat_fan_5678.dta"
{txt}
{com}. 
. 
. keep *cell a_bin a_mid e_* s* // reduce 
{txt}
{com}. 
. rename s6_* v_* // rename the vertical matrix 
{res}{txt}
{com}. 
. preserve 
{txt}
{com}. 
. keep cell a_bin v* 
{txt}
{com}. 
. save wmat_vert_fan, replace 
{txt}{p 0 4 2}
file {bf}
wmat_vert_fan.dta{rm}
saved
{p_end}

{com}. 
. restore 
{txt}
{com}. 
. clear 
{txt}
{com}. 
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced\spatial_3_wmat_4corners_5678.dta"
{txt}
{com}. 
. 
. keep *cell a_bin a_mid e_* s* // reduce 
{txt}
{com}. 
. rename s6_* v_* // rename the vertical matrix 
{res}{txt}
{com}. 
. preserve 
{txt}
{com}. 
. keep cell a_bin v* 
{txt}
{com}. 
. save wmat_vert_4corners, replace 
{txt}{p 0 4 2}
file {bf}
wmat_vert_4corners.dta{rm}
saved
{p_end}

{com}. 
. restore 
{txt}
{com}. 
. clear 
{txt}
{com}. 
. 
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam\spatial_3_wmat_beam_5678.dta"
{txt}
{com}. 
. 
. keep *cell a_bin a_mid e_* s* // reduce 
{txt}
{com}. 
. rename s6_* v_* // rename the vertical matrix 
{res}{txt}
{com}. 
. preserve 
{txt}
{com}. 
. keep cell a_bin v* 
{txt}
{com}. 
. save wmat_vert_beam, replace 
{txt}{p 0 4 2}
file {bf}
wmat_vert_beam.dta{rm}
saved
{p_end}

{com}. 
. restore 
{txt}
{com}. 
. clear 
{txt}
{com}. 
.                 ********************************************************************************
.                 *** 2) + 3) load each of the 6 data sets: merge with vertical, define overlap **
.                 ********************************************************************************
. 
.                 
.                 *** Fan, 5-8 ***
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan\spatial_3_wmat_fan_5678.dta" 
{txt}
{com}. drop m* 
{txt}
{com}. 
. merge 1:1 cell using wmat_vert_fan 
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           4,950{txt}  (_merge==3)
{col 5}{hline 41}

{com}. 
. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         
.         forvalues j = 1/4950 {c -(}
{txt}  3{com}.                 
.                 capture quietly replace s`i'_`j' = 0 if v_`j' == 1
{txt}  4{com}.         
.         {c )-}
{txt}  5{com}.                 
. {c )-}
{txt}
{com}.         
. 
. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell // must by spset. 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_3_wmat_fan_5678.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
. 
.                 *** S5
. spmatrix fromdata S5 = s5_1 - s5_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S5 using S5.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S5} saved in file {bf:S5.stswm}){p_end}

{com}. 
. 
.                 *** S6
. spmatrix fromdata S6 = v_1 - v_4950, normalize(none) // *!* 
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}

{com}. 
. spmatrix save S6 using S6.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S6} saved in file {bf:S6.stswm}){p_end}

{com}. 
.  
.                 *** S7
. spmatrix fromdata S7 = s7_1 - s7_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S7 using S7.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S7} saved in file {bf:S7.stswm}){p_end}

{com}. 
. 
.                 *** S8
. spmatrix fromdata S8 = s8_1 - s8_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S8 using S8.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S8} saved in file {bf:S8.stswm}){p_end}

{com}. 
. clear
{txt}
{com}. 
. 
.                 *** Fan, 1-4 ***
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan\spatial_3_wmat_fan_1234.dta" 
{txt}
{com}. drop m* 
{txt}
{com}. 
. merge 1:1 cell using wmat_vert_fan 
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           4,950{txt}  (_merge==3)
{col 5}{hline 41}

{com}. 
. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         
.         forvalues j = 1/4950 {c -(}
{txt}  3{com}.                 
.                 capture quietly replace s`i'_`j' = 0 if v_`j' == 1
{txt}  4{com}.         
.         {c )-}
{txt}  5{com}.                 
. {c )-}
{txt}
{com}.         
. 
. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell // must by spset. 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_3_wmat_fan_1234.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
.                 *** S1
. spmatrix fromdata S1 = s1_1 - s1_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S1 using S1.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S1} saved in file {bf:S1.stswm}){p_end}

{com}. 
.                 *** S2
. spmatrix fromdata S2 = s2_1 - s2_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S2 using S2.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S2} saved in file {bf:S2.stswm}){p_end}

{com}. 
.  
.                 *** S3
. spmatrix fromdata S3 = s3_1 - s3_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S3 using S3.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S3} saved in file {bf:S3.stswm}){p_end}

{com}. 
. 
.                 *** S4
. spmatrix fromdata S4 = s4_1 - s4_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save S4 using S4.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:S4} saved in file {bf:S4.stswm}){p_end}

{com}. 
. 
. clear
{txt}
{com}. 
.         
.                 *** Corners, 5-8 ***
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced\spatial_3_wmat_4corners_5678.dta" 
{txt}
{com}. capture drop m* 
{txt}
{com}. 
. merge 1:1 cell using wmat_vert_4corners
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           4,950{txt}  (_merge==3)
{col 5}{hline 41}

{com}. 
. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         
.         forvalues j = 1/4950 {c -(}
{txt}  3{com}.                 
.                 capture quietly replace s`i'_`j' = 0 if v_`j' == 1
{txt}  4{com}.         
.         {c )-}
{txt}  5{com}.                 
. {c )-}
{txt}
{com}.         
. 
. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell // must by spset. 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_3_wmat_4corners_5678.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
. 
.                 *** C5
. spmatrix fromdata C5 = s5_1 - s5_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save C5 using C5.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C5} saved in file {bf:C5.stswm}){p_end}

{com}. 
. 
.                 *** C6
. spmatrix fromdata C6 = v_1 - v_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}

{com}. 
. spmatrix save C6 using C6.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C6} saved in file {bf:C6.stswm}){p_end}

{com}. 
.  
.                 *** C7
. spmatrix fromdata C7 = s7_1 - s7_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save C7 using C7.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C7} saved in file {bf:C7.stswm}){p_end}

{com}. 
. 
.                 *** C8
. spmatrix fromdata C8 = s8_1 - s8_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4950 islands){p_end}

{com}. 
. spmatrix save C8 using C8.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C8} saved in file {bf:C8.stswm}){p_end}

{com}. 
. clear 
{txt}
{com}. 
. 
.                 *** Corners, 1-4 ***
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced\spatial_3_wmat_4corners_1234.dta" 
{txt}
{com}. capture drop m* 
{txt}
{com}. 
. merge 1:1 cell using wmat_vert_4corners
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           4,950{txt}  (_merge==3)
{col 5}{hline 41}

{com}. 
. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         
.         forvalues j = 1/4950 {c -(}
{txt}  3{com}.                 
.                 capture quietly replace s`i'_`j' = 0 if v_`j' == 1
{txt}  4{com}.         
.         {c )-}
{txt}  5{com}.                 
. {c )-}
{txt}
{com}.         
. 
. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell // must by spset. 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_3_wmat_4corners_1234.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
.                 *** C1
. spmatrix fromdata C1 = s1_1 - s1_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save C1 using C1.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C1} saved in file {bf:C1.stswm}){p_end}

{com}. 
.                 *** C2
. spmatrix fromdata C2 = s2_1 - s2_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save C2 using C2.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C2} saved in file {bf:C2.stswm}){p_end}

{com}. 
.  
.                 *** C3
. spmatrix fromdata C3 = s3_1 - s3_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save C3 using C3.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C3} saved in file {bf:C3.stswm}){p_end}

{com}. 
. 
.                 *** C4
. spmatrix fromdata C4 = s4_1 - s4_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save C4 using C4.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:C4} saved in file {bf:C4.stswm}){p_end}

{com}. 
. 
. clear 
{txt}
{com}.         
.         
.         
.                 *** Beam, 5-8 ***
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam\spatial_3_wmat_beam_5678.dta" 
{txt}
{com}. capture drop m* 
{txt}
{com}. 
. merge 1:1 cell using wmat_vert_beam
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           4,950{txt}  (_merge==3)
{col 5}{hline 41}

{com}. 
. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         
.         forvalues j = 1/4950 {c -(}
{txt}  3{com}.                 
.                 capture quietly replace s`i'_`j' = 0 if v_`j' == 1 
{txt}  4{com}.         
.         {c )-}
{txt}  5{com}.                 
. {c )-}
{txt}
{com}.         
. 
. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell // must by spset. 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_3_wmat_beam_5678.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
. 
.                 *** B5
. spmatrix fromdata B5 = s5_1 - s5_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save B5 using B5.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B5} saved in file {bf:B5.stswm}){p_end}

{com}. 
. 
.                 *** B6
. spmatrix fromdata B6 = v_1 - v_4950, normalize(none) // *!* 
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}

{com}. 
. spmatrix save B6 using B6.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B6} saved in file {bf:B6.stswm}){p_end}

{com}. 
.  
.                 *** B7
. spmatrix fromdata B7 = s7_1 - s7_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save B7 using B7.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B7} saved in file {bf:B7.stswm}){p_end}

{com}. 
. 
.                 *** B8
. spmatrix fromdata B8 = s8_1 - s8_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}

{com}. 
. spmatrix save B8 using B8.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B8} saved in file {bf:B8.stswm}){p_end}

{com}. 
. clear 
{txt}
{com}. 
. 
.                 *** Beam, 1-4 ***
. use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam\spatial_3_wmat_beam_1234.dta" 
{txt}
{com}. capture drop m* 
{txt}
{com}. 
. merge 1:1 cell using wmat_vert_beam
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}               0
{txt}{col 5}Matched{col 30}{res}           4,950{txt}  (_merge==3)
{col 5}{hline 41}

{com}. 
. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         
.         forvalues j = 1/4950 {c -(}
{txt}  3{com}.                 
.                 capture quietly replace s`i'_`j' = 0 if v_`j' == 1
{txt}  4{com}.         
.         {c )-}
{txt}  5{com}.                 
. {c )-}
{txt}
{com}.         
. 
. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell // must by spset. 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_3_wmat_beam_1234.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
.                 *** B1
. spmatrix fromdata B1 = s1_1 - s1_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save B1 using B1.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B1} saved in file {bf:B1.stswm}){p_end}

{com}. 
.                 *** B2
. spmatrix fromdata B2 = s2_1 - s2_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save B2 using B2.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B2} saved in file {bf:B2.stswm}){p_end}

{com}. 
.  
.                 *** B3
. spmatrix fromdata B3 = s3_1 - s3_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save B3 using B3.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B3} saved in file {bf:B3.stswm}){p_end}

{com}. 
. 
.                 *** B4
. spmatrix fromdata B4 = s4_1 - s4_4950, normalize(none) 
{res}{p 2 2 2}{txt}(weighting matrix contains 4902 islands){p_end}

{com}. 
. spmatrix save B4 using B4.stswm, replace // inverse: spmatrix use 
{res}{p 2 2 2}{txt}(matrix {bf:B4} saved in file {bf:B4.stswm}){p_end}

{com}. 
. 
. clear 
{txt}
{com}. /*      
>                         ***** load all matrices *** 
>                         
> forvalues i = 1/8 {c -(}
>         
>         capture spmatrix use S`i' using S`i'.stswm, replace     
>         capture spmatrix use C`i' using C`i'.stswm, replace     
>         capture spmatrix use B`i' using B`i'.stswm, replace     
>         
>         {c )-}
>                         
> */ 
. 
{txt}end of do-file

{com}. do "C:\Users\hy65byfe\AppData\Local\Temp\STD6b8_000000.tmp"
{txt}
{com}.         *************************************************
.         * 4)  Transform weight matrix to Finished Data **
.         *************************************************
. 
. 
. *cd "R:\WSV2\TBu_AKe\Spatial_NEW"
. cd "C:\Users\hy65byfe\Desktop\smerge_0712"
{res}C:\Users\hy65byfe\Desktop\smerge_0712
{txt}
{com}. use spatial_6_regs_s_monthly, replace
{txt}
{com}. 
. drop if missing(date) // 485,100 obs initially, already cleared here. 
{txt}(485,100 observations deleted)

{com}.  
. *** spset 
. xtset cell kt // kt: cntry-year observation: must be balanced. 
{res}
{col 1}{txt:Panel variable: }{res:cell}{txt: (strongly balanced)}
{p 1 16 2}{txt:Time variable: }{res:kt}{txt:, }{res:{bind:1}}{txt: to }{res:{bind:1176}}{p_end}
{txt}{col 10}Delta: {res}1 unit
{txt}
{com}. spset cell  // set identifier
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_6_regs_s_monthly.dta}
Linked shapefile: <none>
{ralign 16:Data}: Panel
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Time ID}: {res:kt} (see {res:xtset})
{ralign 16:Coordinates}: <none>

{com}.  
. 
. *******************************************************************
.          ** basic reg: index: i = product, k = cell, var kt = country-year pairs
.                 * HERE: n
.                 * log_x_k,t = wx_k,t-1 + log_x_k,t-1 + e_i,t(clustered)
. ********************************************************************
.  
.                         ***************************
.                         ** Fill in the blanks *****
.                         ***************************
. 
. ******** Outcome: sales 
.                 ** lag, 
. *codebook L3_sd // 9k missing
. replace L3_sd = 0 if missing(L3_sd) // assume: missing is a zero.
{txt}(178,200 real changes made)

{com}. replace L3_lsd = 0 if missing(L3_lsd)  // assume: missing as true missing or missing as zero are actually 1 single units. 
{txt}(5,755,246 real changes made)

{com}. 
.                 ** contemp. 
. *codebook log_sd
. replace log_sd = 0 if missing(log_sd) // 286k 
{txt}(5,753,524 real changes made)

{com}. 
. ******** Outcome: count 
.                 ** lag
.                 
. *codebook L3_cd // 9k missing
. replace L3_cd = 0 if missing(L3_cd) // assume: missing is a zero. probably reasonable. 
{txt}(178,200 real changes made)

{com}. replace L3_lcd = 0 if missing(L3_lcd)  // assume: missing as true missing or missing as zero are actually 1 single units. not reasonable. 
{txt}(5,732,475 real changes made)

{com}.                 
.                 ** contemp. 
. *codebook log_cd
. replace log_cd = 0 if missing(log_cd)
{txt}(5,729,921 real changes made)

{com}. 
. 
. ******** Load Matrices
.                         
. spmatrix dir // check active matrices. MANUAL: if cleared all must activate with spmatrix use. 
{res}{txt}
{hline 72}
{txt}{col 4}Weighting matrix name{txt}{col 35} N x N{txt}{col 47}Type{txt}{col 60}Normalization
{txt}{hline 72}
{res}{space 22}B1{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B2{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B3{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B4{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B5{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B6{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B7{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B8{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C1{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C2{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C3{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C4{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C5{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C6{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C7{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C8{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S1{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S2{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S3{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S4{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S5{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S6{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S7{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S8{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{txt}{hline 72}

{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102
{txt}
{com}. 
. forvalues i = 1/8 {c -(}
{txt}  2{com}.         capture spmatrix use S`i' using S`i'.stswm, replace 
{txt}  3{com}.         capture spmatrix use C`i' using C`i'.stswm, replace 
{txt}  4{com}.         capture spmatrix use B`i' using B`i'.stswm, replace 
{txt}  5{com}. {c )-}
{txt}
{com}. 
. spmatrix dir
{res}{txt}
{hline 72}
{txt}{col 4}Weighting matrix name{txt}{col 35} N x N{txt}{col 47}Type{txt}{col 60}Normalization
{txt}{hline 72}
{res}{space 22}B1{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B2{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B3{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B4{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B5{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B6{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B7{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}B8{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C1{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C2{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C3{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C4{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C5{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C6{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C7{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}C8{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S1{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S2{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S3{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S4{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S5{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S6{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S7{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{res}{space 22}S8{res}{space 5}4950 x 4950{res}{col 45}custom{res}{col 62}none
{txt}{hline 72}

{com}. 
. ******** Gen Lags for outcome variables
. 
. 
.  forvalues i = 1/8 {c -(}
{txt}  2{com}.         gen S`i'_L3_sd = 0 
{txt}  3{com}.         gen S`i'_L3_cd = 0 
{txt}  4{com}.         
.         gen C`i'_L3_sd = 0 
{txt}  5{com}.         gen C`i'_L3_cd = 0 
{txt}  6{com}.         
.         gen B`i'_L3_sd = 0 
{txt}  7{com}.         gen B`i'_L3_cd = 0 
{txt}  8{com}. {c )-}
{txt}
{com}.  
. 
. keep if year == 2014 // recall: 'time' is country in the spstructure. 
{txt}(5,405,400 observations deleted)

{com}. 
.                 * loop logic is following: go through 4 outcome vars. 
.                         * for each one, go country by country, create auxiliary spgen
.                         * then combine all into 1 var. drop auxiliaries 
.                         
. 
. egen k14 = group(ccode date) // with monthly data, repeated values in year. new identifier keeps spgen running.         
{res}{txt}
{com}. /* 
> browse if missing(k14)
> tab month if missing(k14)
> */ 
. 
. drop if missing(date) // 34,650, all cleared here already 
{txt}(0 observations deleted)

{com}.         
. sum k14, meanonly  // date ccode group. 
{txt}
{com}. scalar kmin = `r(min)' // store in scalars if sub-sample used later. 
{txt}
{com}. scalar kmax = `r(max)'
{txt}
{com}.         
. scalar list kmin kmax // 1 to 84 
{txt}      kmin = {res}         1
{txt}      kmax = {res}        84
{txt}
{com}.  
.  *** spset 
. xtset cell k14 // kt: cntry-year observation: must be balanced.
{res}
{col 1}{txt:Panel variable: }{res:cell}{txt: (strongly balanced)}
{p 1 16 2}{txt:Time variable: }{res:k14}{txt:, }{res:{bind:1}}{txt: to }{res:{bind:84}}{p_end}
{txt}{col 10}Delta: {res}1 unit
{txt}
{com}. capture spset cell  // set identifier
{txt}
{com}. 
. ** CHECKPOINT: if this works, the data is set up correctly.
. 
{txt}end of do-file

{com}. do "C:\Users\hy65byfe\AppData\Local\Temp\STD6b8_000000.tmp"
{txt}
{com}.  ***  S1 - 8 
.  
. foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S1*`var' if k14==`k'
{txt}  5{com}.          quietly replace S1_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C1*`var' if k14==`k'
{txt}  8{com}.          quietly replace C1_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B1*`var' if k14==`k'
{txt} 11{com}.          quietly replace B1_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-} 
{res}{txt}
{com}.  
.  
.  foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S2*`var' if k14==`k'
{txt}  5{com}.          quietly replace S2_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C2*`var' if k14==`k'
{txt}  8{com}.          quietly replace C2_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B2*`var' if k14==`k'
{txt} 11{com}.          quietly replace B2_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-} 
{res}{txt}
{com}.  
.  
.   foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.       spgenerate Waux_`var'_`i' = S3*`var' if k14==`k'
{txt}  5{com}.          quietly replace S3_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C3*`var' if k14==`k'
{txt}  8{com}.          quietly replace C3_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B3*`var' if k14==`k'
{txt} 11{com}.          quietly replace B3_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-} 
{res}{txt}
{com}.  
.  
.  
.   foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S4*`var' if k14==`k'
{txt}  5{com}.          quietly replace S4_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C4*`var' if k14==`k'
{txt}  8{com}.          quietly replace C4_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B4*`var' if k14==`k'
{txt} 11{com}.          quietly replace B4_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-} 
{res}{txt}
{com}.  
.  
.  foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S5*`var' if k14==`k'
{txt}  5{com}.          quietly replace S5_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C5*`var' if k14==`k'
{txt}  8{com}.          quietly replace C5_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B5*`var' if k14==`k'
{txt} 11{com}.          quietly replace B5_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-} 
{res}{txt}
{com}.  
.  
.  foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S6*`var' if k14==`k'
{txt}  5{com}.          quietly replace S6_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C6*`var' if k14==`k'
{txt}  8{com}.          quietly replace C6_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B6*`var' if k14==`k'
{txt} 11{com}.          quietly replace B6_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-} 
{res}{txt}
{com}. 
.  
.  
. foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S7*`var' if k14==`k'
{txt}  5{com}.          quietly replace S7_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C7*`var' if k14==`k'
{txt}  8{com}.          quietly replace C7_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B7*`var' if k14==`k'
{txt} 11{com}.          quietly replace B7_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-}  
{res}{txt}
{com}.  
.  
.  
. foreach var of varlist L3_sd L3_cd {c -(}
{txt}  2{com}.         forvalues i = `=kmin'/`=kmax' {c -(}
{txt}  3{com}.         local k = `i' 
{txt}  4{com}.                 
.       spgenerate Waux_`var'_`i' = S8*`var' if k14==`k'
{txt}  5{com}.          quietly replace S8_`var'=Waux_`var'_`i' if k14==`k'
{txt}  6{com}.                  drop Waux* 
{txt}  7{com}.                  
.            spgenerate Waux_`var'_`i' = C8*`var' if k14==`k'
{txt}  8{com}.          quietly replace C8_`var'=Waux_`var'_`i' if k14==`k'
{txt}  9{com}.                  drop Waux* 
{txt} 10{com}.                  
.                 spgenerate Waux_`var'_`i' = B8*`var' if k14==`k'
{txt} 11{com}.          quietly replace B8_`var'=Waux_`var'_`i' if k14==`k'
{txt} 12{com}.                  drop Waux* 
{txt} 13{com}.          {c )-} 
{txt} 14{com}.  {c )-}  
{res}{txt}
{com}.  
.  
. browse cell year ccode S* C* B* L3_sd L3_cd if cell <= 50 & year == 2014 // looks ok? 
{txt}
{com}. 
. save spatial_regs_s_monthly_0102_overlap, replace
{txt}{p 0 4 2}
(file {bf}
spatial_regs_s_monthly_0102_overlap.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_regs_s_monthly_0102_overlap.dta{rm}
saved
{p_end}

{com}. 
. clear
{txt}
{com}. 
{txt}end of do-file

{com}. cd
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_SCB_overlap_0102

{com}. clear

. 